const app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    src: "", //图片的链接
    baidutoken: "",
    base64: "",
    msg: ""
  },

  //拍照
  takePhoto() {
    var that = this;
    //拍照
    const ctx = wx.createCameraContext()
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
       
        let tempfilepath = res.tempFiles[0].tempFilePath
        that.setData({
          src: tempfilepath //获取图片
         })
        wx.getFileSystemManager().readFile({
          filePath: tempfilepath, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            console.log(res)
            that.uploadPhoto(res.data)
            that.setData({
              base64: res.data
            })
          }
        })
        
      }
    })
    // ctx.takePhoto({
    //   quality: 'high',
    //   success: (res) => {
    //     this.setData({
    //       src: res.tempImagePath //获取图片
    //     })
        //图片base64编码
        // wx.getFileSystemManager().readFile({
        //   filePath: this.data.src, //选择图片返回的相对路径
        //   encoding: 'base64', //编码格式
        //   success: res => { //成功的回调
        //     console.log(res)
        //     this.setData({
        //       base64: res.data
        //     })
        //   }
        // })
    //     // this.getBaiduToken();
    //   } //拍照成功结束
    // }) //调用相机结束

    //失败尝试
    // wx.showToast({
    //   title: '请重试',
    //   icon: 'loading',
    //   duration: 500
    // })
  },
  error(e) {
    console.log(e.detail)
  },

  // getBaiduToken() {
  //   var that = this;
  //   //acess_token获取,qs:需要多次尝试
  //   wx.request({
  //     url: 'https://aip.baidubce.com/oauth/2.0/token', //是真实的接口地址
  //     data: {
  //       grant_type: 'client_credentials',
  //       client_id: 'xxxxxxxxxxxxxxxxxxx', //用你创建的应用的API Key
  //       client_secret: 'xxxxxxxxxxxxxxxxxxx' //用你创建的应用的Secret Key
  //     },
  //     header: {
  //       'Content-Type': 'application/json' // 默认值
  //     },
  //     success(res) {
  //       that.setData({
  //         baidutoken: res.data.access_token //获取到token
  //       })
  //       that.uploadPhoto();
  //     }
  //   })
  // },

  uploadPhoto(data) {
    var that = this;
    //上传人脸进行注册-----test
    let base = that.data.base64
    let token = wx.getStorageSync('token')
    console.log(base)
    wx.request({
      url: 'http://www.house.com/index.php/api/face',
      method: 'POST',
      header:{
        token:token
      },
      data: {
        image: data,
        type: 'base64',
         //自己建的用户组id
      },
      success(res) {
        console.log(res)
        that.setData({
          msg: res.data.error_msg
        })
        //做成功判断
        if (that.data.msg == "pic not has face") {
          wx.showToast({
            title: '未捕获到人脸',
            icon: 'error',
          })
        }
        if (that.data.msg == 'SUCCESS') {
          wx.showToast({
            title: '人脸录入成功',
            icon: 'success',
          })
        }
      }
    })
  },
})
